package com.acme.bankapp.service.bank;

import java.util.ArrayList;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.exceptions.ClientExistException;
import com.acme.exceptions.NotEnoughFundsException;

public class BankService {
	public Bank bank;

	private final static String savingAccount = "Saving account: ";
	private final static String checkingAccount = " Checking account: ";

	public BankService() {
		ArrayList<ClientRegistrationListener> arrayList = new ArrayList<ClientRegistrationListener>();
		arrayList.add(new PrintClientListener());
		arrayList.add(new EmailNotificationListener());

		this.bank = new Bank(arrayList);
	}

	public void addClient(Client client) throws ClientExistException {
		for (Client c : bank.clients) {
			if (c.equals(client)) {
				throw new ClientExistException();
			}
		}
		bank.clients.add(client);
		for (ClientRegistrationListener listener : bank.listeners) {
			listener.onClientAdded(client);
		}

	}

	public void modifyBank(Client client, double amount, boolean meth)
			throws NotEnoughFundsException {
		if (meth == true) {
			client.accountS.deposit(amount);
		} else {
			client.accountC.withdraw(amount);
		}
	}

	public void printBalance() {
		for (Client client : bank.clients) {
			if (client == null) {
				break;
			}
			System.out.println(client);

		}
	}

	public void printMaximumAmountToWidthdraw(Client client) {
		System.out.print(savingAccount
				+ client.accountS.maximumAmountToWithdraw());
		System.out.println(checkingAccount
				+ client.accountC.maximumAmountToWithdraw());
	}

	public class EmailNotificationListener implements
			ClientRegistrationListener {
		private final static String before = "Notification email for client ";
		private final static String after = " to be sent";

		@Override
		public void onClientAdded(Client client) {
			System.out.println(before + client.getName() + after);
		}

	}

	public class PrintClientListener implements ClientRegistrationListener {

		@Override
		public void onClientAdded(Client client) {
			System.out.println(client);
		}

	}

}
